package com.luwak.controller;

import com.luwak.entity.UserEntity;
import com.luwak.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @author ChenXc
 * @version V1.0
 * @Date 2018/2/27  16:41
 * @Description TODO(一句话描述类作用)
 */
@Controller
@AllArgsConstructor
public class HomeController {

    private final UserService userService;

    @GetMapping({"/", "/index", "/home"})
    public String root(){
        return "index";
    }

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @PostMapping("/register")
    public String doRegister(UserEntity userEntity){

        // 此处省略校验逻辑
        //加密密码
        String password = userEntity.getPassword();
        password = new BCryptPasswordEncoder().encode(password);
        userEntity.setPassword(password);

        if (userService.insert(userEntity))
            return "redirect:register?success";
        return "redirect:register?error";
    }
}
